<?php
if(!defined('BLOG'))exit('不作死就不会死');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/1/16
 * Time: 14:09
 */
base::load_class('session_mem','',1);
base::load_class('cookie');
class login{
    private $db;
    public function __construct(){
        $this->db = base::load_model('users_model');
    }
    public function userlogin(){
        $forminfo = my_addslashes($_POST);

        $email = $forminfo['username'];
        $passwd = $forminfo['passwd'];
        $captcha = $forminfo['captcha'];


        //验证码
        if ($captcha != strtolower($_SESSION['captcha'])) {
            echo json_encode(array(L('captcha_error')));exit;
        }
        //电子邮件
        if (!preg_match('/^[a-zA-Z\-_\d\.]+@[a-z\d]+\.[a-z\.]{2,6}/', $email)){
            echo json_encode(array(L('email_error')));exit;
        }
        $where  =  'where mail = '."'{$email}'";
        $repasswd = $this->db->get_one(array('passwd','secretkey'),$where);
        //用户
        if (empty($repasswd)){echo json_encode(array(L('user_not_exists')));exit;}
        //密码
        if ( hash("haval160,4",strmcrypt($passwd,$repasswd['secretkey']))!= $repasswd['passwd']){
            echo json_encode(array(L('passwd_error')));exit;
        }
        $_SESSION['userlogin']=true;
        cookie::set_cookie("username",$email);
        cookie::set_cookie("login",true,'','','','',$mcrpty=false);
        echo json_encode(array(true));
    }
}